#!/usr/bin/env python
# coding=utf-8
# __author__ = 'Yunchao Ling'

from pymongo import MongoClient


def InitDB():
    SERVER = 'localhost'
    PORT = 27017
    DB_NAME = "metacyc"
    COLLECTION_NAME = "classes"
    global connection
    connection = MongoClient(SERVER, PORT)
    db = connection[DB_NAME]
    global collection
    collection = db[COLLECTION_NAME]


def distinctDB(key, parent, ht):
    tmpString = parent + "->" + key
    if not ht.has_key(key):
        result2 = collection.find_one({"UNIQUE-ID": key})
        commonName = ""
        if result2.has_key("COMMON-NAME"):
            commonName = result2["COMMON-NAME"]
        print key + "\t" + commonName + "\t" + tmpString[2:]
    results = collection.distinct("UNIQUE-ID", {"TYPES": key})
    for result in results:
        distinctDB(result, tmpString, ht)


if __name__ == "__main__":
    InitDB()
    ht = {}
    distinctDB("Compounds", "", ht)
    connection.close()
